package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.constant.CiccTableNameEnum;
import com.chinairi.mall.dao.ProjectPlanReqMapper;
import com.chinairi.mall.model.ProjectPlanReq;
import com.chinairi.mall.model.ReqModelDetail;
import com.chinairi.mall.model.ServiceOrderProject;
import com.chinairi.mall.dao.ServiceOrderProjectMapper;
import com.chinairi.mall.service.IServiceOrderProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.util.CiccRecordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 项目服务订单表 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-09-15
 */
@Service
public class ServiceOrderProjectServiceImpl extends ServiceImpl<ServiceOrderProjectMapper, ServiceOrderProject> implements IServiceOrderProjectService {


    @Autowired
    private ServiceOrderProjectMapper serviceOrderProjectMapper;

    @Autowired
    private ProjectPlanReqMapper projectPlanReqMapper ;


    /**
     * 获取项目服务订单列表
     *
     * @param serviceOrderProject
     * @return
     */
    @Override
    @DoPage
    public List<ServiceOrderProject> getList(ServiceOrderProject serviceOrderProject) {
        QueryWrapper<ServiceOrderProject> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotEmpty(serviceOrderProject.getSopUserUnit()), "SOP_USER_UNIT", serviceOrderProject.getSopUserUnit())
                .like(StringUtils.isNotEmpty(serviceOrderProject.getSopType()), "SOP_TYPE", serviceOrderProject.getSopType())
                .orderByAsc("SOP_STATUS").orderByDesc("CREATE_TIME");
        List<ServiceOrderProject> serviceOrderProjectList = serviceOrderProjectMapper.selectList(queryWrapper);
        if (serviceOrderProjectList !=null &&!serviceOrderProjectList.isEmpty()){
            serviceOrderProjectList.forEach(serviceOrderProjectNew -> {
                serviceOrderProjectNew.setSopStatusLabel(CiccRecordUtil.getCiccRecord(CiccTableNameEnum.ORDER_CONFIRM_STATUS.getCodeValue(),String.valueOf(serviceOrderProjectNew.getSopStatus())).getLabel());
                ProjectPlanReq projectPlanReq = serviceOrderProjectMapper.getProjectPlanReq(serviceOrderProjectNew.getpId());
                if (projectPlanReq !=null) {
                    serviceOrderProjectNew.setPlanReqId(projectPlanReq.getPlanReqId());
                    serviceOrderProjectNew.setPlanReqStatus(projectPlanReq.getPlanReqStatus());
                    serviceOrderProjectNew.setPlanReqApproveSatus(projectPlanReq.getApproveStatus());
                }
            });
        }
        return serviceOrderProjectList;
    }

    /**
     * 根据模型id获取模型明细
     *
     * @param modelId
     * @return
     */
    @Override
    public List<ReqModelDetail> getModelList(String modelId) {
        return serviceOrderProjectMapper.getReqModelDetailByModelId(modelId);
    }
}
